/*

    This file is part of GLua

    GLua Development Environment
    Copyright (C) 2007 VoiDeD

    GLua is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    GLua is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

// !!! IMPORTANT !!!
// If you get alot of warnings related to documentation or undocumented public members you can ignore them or uncheck 'documentation' in project settings
// !!! IMPORTANT !!!

/*
 * I'm required to say that you are not allowed to build this code without licensed sanddock and qwhale editor.net components
 * How you get these components is completely up to you. (Legally, of course...)
 * If you need any help, you may contact me on the facepunch forums - voided
 * 
 * I'm also fully aware that I probably shouldn't be releasing this source based on my licenses, but I'm taking the risk
 * to give back to the community
 * 
 * If you're currently looking at this text, I'm going to assume you have some programming knowledge
 * And if not, this will be challenging
 * 
 * GLua is laid out in a somewhat simple fashion:
 * 
 * 1) The application checks if any arguments were passed to it, such a filenames
 *    It checks these filenames and determines if they're lua files or glua projects
 *    If a .lua file is detected, the program attempts to find an existing GLua instance
 *    in order to display the file there, if another instance couldn't be found, it starts a new one
 *    If a .glu project is detected, it is always opened in a new instance
 * 
 * 2) a main window is created with an EnvironmentManager class attached to it
 *    This environment manager does everything you'd need to do various things with the user interface
 *    The environment manager class is the most important class in the project, almost everything happens
 *    there
 * 
 * 3) Events from the window are managed by the window, and various environment calls are made to
 *    handle these events
 * 
 * 4) All execution happens in the window and ends there as well
 * 
 * If you are looking for things such as syntax highlighting, or the code database, check  \bin\<debug|release>\data\
 * 
 * If you are interested in continuing this project and maybe even finishing it, your best bet
 * would be to follow program execution, and see where things happen
 * 
 * I've tried to comment code as best I could, but there might be some things that are not commented properly
 * 
 * If you have any questions related to this code, you can contact me at:
 * 
 * email - voidedweasel@gmail.com
 * aim - voidedmaster
 * facepunch - voided
 * irc - irc.gamesurge.net / #glua or #luahelp
 *
 * A small note I want to add, is that if you want access to resource content, just open \Properties\Resources.resx\
 * You'll find that everything is included inside the resource file and you'll be able to export anything you like
 *
 * Alot of the art (button/menu icons mostly) has been ripped from Visual Studio, but some art
 * such as the about dialog graphics and .lua/.glu icons are from a few of my friends
 * You can find them on Facepunch - Exilelord and Danke, both are skilled in what they do so if you ever seem them, tell them thanks!
 *
*/

namespace GLua
{
    public class Program
    {
        [STAThread]
        public static void Main( string[] args )
        {
            Application.EnableVisualStyles();

            try
            {
                if ( args.Length > 0 )
                {
                    // huge class name 
                    IntPtr hwnd = Util.FindWindow( "WindowsForms10.Window.8.app.0.378734a", "GLua" );
                    if ( hwnd != IntPtr.Zero ) // we have a valid handle
                    {
                        FileInfo fi = null;
                        try
                        {
                            fi = new FileInfo( args[0] );
                            if ( !fi.Exists )
                                return;
                        }
                        catch
                        {
                            // invalid file, we return and don't do anything
                            return;
                        }
                        if ( fi.Extension == ".lua" ) // just opening an lua file
                        {
                            /* 
                             * message format is like this:
                             * 
                             * <START> WM_GLUA_START
                             * "f" WM_GLUA_OPENFILE
                             * "i" WM_GLUA_OPENFILE
                             * "l" WM_GLUA_OPENFILE
                             * "e" WM_GLUA_OPENFILE
                             * "." WM_GLUA_OPENFILE
                             * "l" WM_GLUA_OPENFILE
                             * "u" WM_GLUA_OPENFILE
                             * "a" WM_GLUA_OPENFILE
                             * <END> WM_GLUA_END
                             * 
                             * Marshal.Copy was failing on me so I decided on this method
                             * this message is handled by the main window, look in the WndProc
                            */
                            Util.SendMessage( hwnd, Util.WM_GLUA_START, 0, 0 );
                            char[] file = args[0].ToCharArray();
                            for ( int x = 0 ; x < file.Length ; x++ )
                            {
                                Util.SendMessage( hwnd, Util.WM_GLUA_OPENFILE, (uint)file[x], 0 );
                            }
                            Util.SendMessage( hwnd, Util.WM_GLUA_END, 0, 0 );
                        }
                        else if (fi.Extension == ".glu") // project should be opened in a new glua window
                        {
                            // project detected, start a new instance
                            Application.Run( new MainForm( args[0] ) );
                        }
                    }
                    else // glua isn't open yet
                    {
                        // should actually start now
                        Application.Run( new MainForm( args[0] ) );
                    }
                }
                else
                    // no arguments, start glua normally
                    Application.Run( new MainForm() );
            }
            catch ( Exception ex )
            {
                // something broke, so display crash dialog
                new ErrorDialog( ex ).ShowDialog();
            }
        }
    }
}
